package com.errol.generic.base;

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
 * Created by Errol on 17/8/6.
 * 系统初始化器，隐藏了 context 各类 event 之间的差异，各项服务需要初始化时继承该类即可
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2020/10/21
 * @since v4.7.6
 */
@Component
public abstract class InitServiceImpl implements ApplicationListener<ApplicationReadyEvent> {

    private boolean initiated = false;

    @Override
    public void onApplicationEvent(@Nullable ApplicationReadyEvent event) {
        if(!initiated) {
            initiated = true;
            init();
        }
    }

    /**
     * 继承实现该方法以便在系统启动时执行需要的逻辑
     */
    protected abstract void init();
}
